(function($,window,system){
	var basePathUrl,pathUrl,prizeCodeUrl,code;
	var light = true;
	
	window.system = system;
	window.system.init = function(){
		basePathUrl = window.basePathUrl;
		pathUrl = basePathUrl+"js/prize/";
		prizeCodeUrl = basePathUrl+"/api/wx/accept";

		code = window.code;
	};
	var state = {underway:false};
	var prize = {
		Motorcycle : {id:0,text:"品牌摩托车",src:'4.png',src1:'4.jpg',units:'辆'},
		Iphone : {id:1,text:"iphone 7",src:'5.png',src1:'5.jpg',units:'台'},
		Again1 : {id:2,text:"再来一次",type:'2'},
		Balance : {id:3,text:"品牌平衡车",src:'6.png',src1:'6.jpg',units:'辆'},
		Glove : {id:4,text:"品牌手套",src:'2.png',src1:'2.jpg',units:'双'},
		Electrombile : {id:5,text:"品牌电动车",src:'3.png',src1:'3.jpg',units:'辆'},
		Again2 : {id:6,text:"再要一次",type:'1'},
		Helmet : {id:7,text:"品牌头盔",src:'1.png',src1:'1.jpg',units:'顶'},
	};
	var lottery = {
		index:-1,	
		count:0,	
		timer:0,	
		speed:20,	
		times:0,	
		cycle:30,	
		order : {},
		init:function(id){
			if ($("#"+id).find(".lottery-unit").length > 0) {
				$lottery = $("#"+id);
				$units = $lottery.find(".lottery-unit");
				this.obj = $lottery;
				this.count = $units.length;
				$lottery.find(".lottery-unit-"+this.index).addClass("active");
			}
			this.num();
			
		},
		roll:function(){
			var index = this.index;
			var count = this.count;
			var lottery = this.obj;
			$(lottery).find(".lottery-unit-" + index).removeClass("active");
			index += 1;
			if (index > count-1) {
				index = 0;
			}
			$(lottery).find(".lottery-unit-" + index).addClass("active");
			
			this.index = index;
			return false;
		},
		stop:function(name){
			this.prize = prize[name].id;
			this.order = prize[name];
			return false;
		},
		start : function(){
			state.qualification = true;
			if(!state.qualification){
				alert("你已经抽过奖了");
				return false;
			}
			else if(state.underway) {
				return false;
			}else{
				lottery.speed = 100;
				roll();
				state.underway = true;
				return false;
			}
		},
		getPrizeHtml : function(type){
			if(type){
				var html = ['<div class="prizeBox clearfix"><div class="prize-1 clearfix">'];
				html.push('<img src="' + pathUrl + 'images/prize/' + this.order.src + '" height="55" />');
				html.push('<img src="' + pathUrl + 'images/icon.png" width="20" />');
				html.push('<img src="' + pathUrl + 'images/icon-1.png" width="100" />');
				html.push('</div><div class="prize-2">');
				html.push('<p>恭喜你获得' + this.order.text + '一' + this.order.units + '</p>');
				html.push('<p>新人优惠券一张</p>');
				html.push('<a href="javascript:accept(1);">知道了</a></div></div>');
				state.qualification = false;
			}
			else{
				var html = ['<div class="prizeBox-2" type="' + this.order.type + '"></div>'];
				html.push('<div class="prize-2"><a href="javascript:accept(0);">' + this.order.text + '</a></div>');
				state.qualification = true;
			}
			$("#prizeBox").html(html.join('')).show();
		},
		num : function(){
			if(code == "0"){
				state.qualification = true;
			}
			else{
				state.qualification = false;
			}
		}
	};
	function roll(){
		lottery.times += 1;
		lottery.roll();
		if(lottery.times > lottery.cycle + 10 && lottery.prize == lottery.index) {
			clearTimeout(lottery.timer);
			lottery.times = 0;			
			state.underway = false;
			setTimeout(function(){
				if(lottery.order.src != null){
					lottery.getPrizeHtml(true);
				}
				else{
					lottery.getPrizeHtml(false);

				}
			},500);
		}else{
			if(lottery.times < lottery.cycle){
				lottery.speed = lottery.speed - 10;
			}else if(lottery.times == lottery.cycle) {	
				lottery.prizeCode = null;
				$.ajax({
					type:"get",
					url:basePathUrl+"/api/wx/dial/extract",
					async:true,
					success : function(data){
						if(data.code == 1){
							var data = eval("("+data.data+")");
							lottery.stop(data.dialCode);
							lottery.order.dialCode = data.dialCode;
							lottery.order.prizeCode = data.prizeCode;
							lottery.order.isNew = '1';
						}
						else{
							alert(data.mem);
							clearTimeout(lottery.timer);
						}
					}
				});
			}else{
				if (lottery.times > lottery.cycle + 10 && ((lottery.prize == 0 && lottery.index == 7) || lottery.prize == lottery.index + 1)) {
					lottery.speed = lottery.speed + 110;
				}else{
					lottery.speed = lottery.speed + 20;
				}
			}
			if (lottery.speed < 40) {
				lottery.speed = 40;
			}
			lottery.timer = setTimeout(roll,lottery.speed);
		}
		return false;
	}
	window.shake = function(){
		if(window.DeviceMotionEvent) {
			var speed = 25;
			var x = y = z = lastX = lastY = lastZ = 0;
			window.addEventListener('devicemotion', function(event){
				var acceleration = event.accelerationIncludingGravity;
				x = acceleration.x;
				y = acceleration.y;
				z = acceleration.z;
				if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed ) {
					lottery.start();
					$("#prizeBox").hide();
				}
				lastX = x;
				lastY = y;
				lastZ = z;
			}, false);
		}
	};
	window.accept = function(type){
		if(type == 1){
			window.location.href = prizeCodeUrl + "?prizeCode=" + lottery.order.prizeCode + '&type=' + lottery.order.dialCode + '&isNew=' + lottery.order.isNew;
			$("#prizeBox").hide();
		}
		else{
			lottery.start();
			$("#prizeBox").hide();
		}
	};
	$.fn.myScroll = function(options){
		var defaults = {
			speed:40,  
			rowHeight:24 
		};
		var opts = $.extend({}, defaults, options),intId = [];
		function marquee(obj, step){
			obj.find("ul").animate({
				marginTop: '-=1'
			},0,function(){
				var s = Math.abs(parseInt($(this).css("margin-top")));
				if(s >= step){
					$(this).find("li").slice(0, 1).appendTo($(this));
					$(this).css("margin-top", 0);
				}
			});
		}
		this.each(function(i){
			var sh = opts["rowHeight"],speed = opts["speed"],_this = $(this);
			intId[i] = setInterval(function(){
				if(_this.find("ul").height()<=_this.height()){
					clearInterval(intId[i]);
				}else{
					marquee(_this, sh);
				}
			}, speed);
		});	
	};
	var UrlSearch = function(){
		var name,value; 
		var str = location.href; 
		var num	= str.indexOf("?");
		str = str.substr(num + 1);
		var arr = str.split("&");
		var par = {};
		for(var i = 0;i < arr.length;i++){ 
			num = arr[i].indexOf("="); 
			if(num > 0){ 
 				name = arr[i].substring(0, num);
 				value = arr[i].substr(num + 1);
 				par[name] = value;
 			} 
		} 
		return par;
	};
	window.lottery = lottery;
	window.getPrizeByCode = function(){
		var arr = {
			prizeCode : window.prizeCode,
			type : window.type,
			isNew : '1'
		};
		var prizeCode = arr.prizeCode;
		var type = arr.type;
		var isNew = arr.isNew;
		var html = ['<p>恭喜您获得<span>' + prize[type].text + '</span>及<span>新人优惠券</span></p>'];
		html.push('<p><img src="'+pathUrl+'images/prize/' + prize[type].src +'" width="70" />');
		if(isNew != '' && isNew != null){
			html.push('<span class="on">+</span><img src="'+pathUrl+'images/icon-4.png" width="100" />');
		}
		html.push('</p>');
		html.push('<p>获取验证码：<span class="code">' + prizeCode + '</span></p>');
		$("#getPrize").html(html.join(''));
		return {
			text : prize[type].text,
			src : prize[type].src1
		};
	}
})($,window,{});
	